package bean;

import java.io.Serializable;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.ArrayList;
import java.util.Hashtable;

import rmi.ServeurRmiInterface;

import annotation.*;

public class Patient implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private static int port = 54000;
	
	private int id;

	@Form (type="text", label="nom")
	@Req (mess="Veuillez saisir un nom")
	private String nom = "";
	
	@Form (type="text", label="prenom")
	@Req (mess="Veuillez saisir un prenom")
	private String prenom = "";
	
	@Form (type="text", label="date")
	@Req (mess="Veuillez saisir une date de naissance")
	private String dateNaissance = null;
	
	@Form (type="text", label="adresse")
	@Req (mess="Veuillez saisir une adresse")
	private String adresse = "";
	
	@Form (type="text", label="codePostal")
	@Req (mess="Veuillez saisir un code postal")
	private int codePostal;
	
	@Form (type="text", label="ville")
	@Req (mess="Veuillez saisir un ville")
	private String ville = "";
	
	@Form (type="text", label="telephone")
	@Req (mess="Veuillez saisir un numero de telephone")
	private int numTel;
	
	@Form (type="text", label="poids")
	@Req (mess="Veuillez saisir un prenom")
	private float poids;
	
	private int jour;
	private int mois;
	private int annee;
	
	Hashtable <String,String> err = new Hashtable<String, String>();
	
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public Hashtable<String, String> getErr() {
		return err;
	}

	public void setErr(Hashtable<String, String> err) {
		this.err = err;
	}

	public String getNom() {
		return nom;
	}

	public void setNom(String nom) {
		this.nom = nom;
	}

	public String getPrenom() {
		return prenom;
	}

	public void setPrenom(String prenom) {
		this.prenom = prenom;
	}

	public String getDateNaissance() {
		return dateNaissance;
	}

	public void setDateNaissance(String dateNaissance) {
		this.dateNaissance = dateNaissance;
	}

	public String getAdresse() {
		return adresse;
	}

	public void setAdresse(String adresse) {
		this.adresse = adresse;
	}

	public int getCodePostal() {
		return codePostal;
	}

	public void setCodePostal(int codePostal) {
		this.codePostal = codePostal;
	}

	public String getVille() {
		return ville;
	}

	public void setVille(String ville) {
		this.ville = ville;
	}

	public int getNumTel() {
		return numTel;
	}

	public void setNumTel(int numTel) {
		this.numTel = numTel;
	}

	public float getPoids() {
		return poids;
	}

	public void setPoids(float poids) {
		this.poids = poids;
	}
	
	public int getJour() {
		return jour;
	}

	public void setJour(int jour) {
		this.jour = jour;
	}

	public int getMois() {
		return mois;
	}

	public void setMois(int mois) {
		this.mois = mois;
	}

	public int getAnnee() {
		return annee;
	}

	public void setAnnee(int annee) {
		this.annee = annee;
	}
	
	public void createDate() {
		dateNaissance = new String(""+annee+"-"+mois+"-"+jour);
		System.out.println(dateNaissance);
	}

	public static ArrayList<Patient> listePatient() {
		try {
			Registry registry = LocateRegistry.getRegistry(port);
			ServeurRmiInterface serveur = (ServeurRmiInterface) registry.lookup("ServeurRMI");
			return serveur.liste_patient();
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NotBoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	public static boolean enregistrerPatient(Patient patient) {
		try {
			Registry registry = LocateRegistry.getRegistry(port);
			ServeurRmiInterface serveur = (ServeurRmiInterface) registry.lookup("ServeurRMI");
			return serveur.enregistrer_patient(patient);
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NotBoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
	
	public static Patient getPatient(int idPatient) {
		try {
			Registry registry = LocateRegistry.getRegistry(port);
			ServeurRmiInterface serveur = (ServeurRmiInterface) registry.lookup("ServeurRMI");
			return serveur.getPatient(idPatient);
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NotBoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	public static boolean suprPatient(int idPatient) {
		try {
			Registry registry = LocateRegistry.getRegistry(port);
			ServeurRmiInterface serveur = (ServeurRmiInterface) registry.lookup("ServeurRMI");
			return serveur.suprPatient(idPatient);
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NotBoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
	
	public static boolean modifierPatient(Patient patient) {
		try {
			Registry registry = LocateRegistry.getRegistry(port);
			ServeurRmiInterface serveur = (ServeurRmiInterface) registry.lookup("ServeurRMI");
			return serveur.modifier_patient(patient);
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NotBoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
}
